<template>
	<view>
		<view>
			<forum-dynamic :datalist="d" @handleQuery="handleQuery" @onJoin="dtJoin" @getLike="getLike"
				:showSearch="search" :showDel="del" :isJumpTo="isJumpTo" :showSubscribe="subscribe" @jumpTo="jumpTo" :bgColor="bgColor">
			</forum-dynamic>
		</view>
		<view style="padding: 30rpx 0;">
			<u-loadmore :status="loading?'loading':'nomore'" />
		</view>
	</view>
</template>

<script>
	import ForumDynamic from "@/components/forum/forum-dynamic.vue"
	import {
		sqyh
	} from "@/api/forum/subscribe.js";
	import {
		listDynamic
	} from "@/api/forum/dynamic.js";
	import {
		zanSave
	} from "@/api/forum/zan.js"
	
	let isChange = false;
	export default {
		components: {
			ForumDynamic,
		},
		props: {
			//默认所有动态，gzrdt关注人动态，wddt我的动态,wdsc我的收藏
			type: String,
			// 显示查询框
			search: {
				type: Boolean,
				default: false
			},
			// 显示删除
			del: {
				type: Boolean,
				default: false
			},
			// 显示关注
			subscribe: {
				type: Boolean,
				default: true
			},
			//跳转用户主页
			isJumpTo: {
				type: Boolean,
				default: false
			},
			bgColor: {
				type: String,
				default: '#f2f2f2'
			},
			// 问答标签参数
			labelId: {
				type: Number
			},
			// 发布者id
			wxUserId: Number
		},
		name: "forum-dynamic-list",
		data() {
			return {
				d: {
					data: [],
					loadStatus: ''
				},
				list: [],
				total: 0,
				queryParams: {
					content: undefined,
					pageSize: 10,
					pageNum: 1,
					queryUserId: uni.getStorageSync('uid'),
					dataType: '0'
				},
				loading: true
			};
		},
		created() {
			this.getList();
		},
		watch: {
			list(val) {
				this.d.data = val;
			}
		},
		methods: {
			getList(){
				this.loading = true;
				if(this.type && this.type == 'gzrdt'){	//关注人的动态
					this.queryParams.isSubscribe = 1;
				}
				if(this.type && this.type == 'wddt'){	//我的动态
					if (this.wxUserId > 0) {
						this.queryParams.wxUserId = this.wxUserId
					}
				}
				if(this.type && this.type == 'wdsc'){	//我的收藏
					this.queryParams.isCollect = 1;
				}
				if (this.type && this.type == "wd") {		
					if (this.labelId > 0) {		// 根据问答标签查询
						this.queryParams.labelId = this.labelId
					}
					if (this.wxUserId > 0) {	// 根据发布者标签查询
						this.queryParams.wxUserId = this.wxUserId;
					}
					this.queryParams.dataType = '1'
				}
				
				listDynamic(this.queryParams).then(res=>{
					console.log('动态列表',res)
					if(res.data.code == 200){
						this.list = this.list.concat(res.data.rows);
						this.total = res.data.total;
					}
					this.loading = false;
				})
			},
			/**
			 * 跳转点击回调
			 * @param {Object} item 对象
			 * @param {Object} type 类型：0点击用户头像，1点击内容
			 */
			jumpTo(item,type){
				let url = "";
				// 允许点击用户头像进入用户主页
				if (this.isJumpTo && type == 0) {
					url = "/pages/community/pages/concernsInfo/concernsInfo?id=" + item.wxUserId
				} else {
					if (this.type == "wd") {	// 跳转问答详情
						url = "/pages/community/pages/ask/problemInfo?id=" + item.id
					} else {	// 动态详情
						url = "/pages/community/pages/topic/topicInfo?id=" + item.id
					}
				}
				uni.navigateTo({
					url: url
				})
			},
			//查询
			query(value) {
				console.log('查询', value)
				if (value) {
					this.queryParams.content = value;
					this.queryParams.pageNum = 1;
					this.d.data = []
					this.getList();
				}
			},
			//刷新
			refresh() {
				this.reset();
				this.getList();
			},
			//下页
			next() {
				if (this.total > this.queryParams.pageNum * this.queryParams.pageSize) {
					this.queryParams.pageNum++;
					this.getList();
				}
			},
			// 社区话题关注、取消
			dtJoin(dataId) {
				uni.showLoading({
					mask: true
				})
				var params = {
					wxUserId: uni.getStorageSync("uid"),
					dataId: dataId
				}
				sqyh(params).then(res => {
					uni.hideLoading()

					if (res.data.code == 200) {
						let datalist = this.d.data;
						datalist.forEach(item => {
							if (item.wxUserId == dataId) {
								if (item.isSubscribe == 1) {
									item.isSubscribe = 0;
									// 我关注的人列表
								} else {
									item.isSubscribe = 1;
								}
							}
						})
						this.d.data = datalist;
					} else {
						uni.showToast({
							title: '操作失败',
							icon: 'none'
						})
					}
				})
			},
			//点赞
			getLike(index) {
				let data = this.d.data[index];
				
				// 双数不提交，单数提交
				isChange = !isChange;
				if (data.isZan == 0) {
					//点赞成功调用后台接口
					data.zanNum++;
					data.isZan = 1;
				} else {
					//点赞取消调用后台接口
					data.zanNum--;
					data.isZan = 0;
				}
				let params = {
					dataId: data.id,
					wxUserId: uni.getStorageSync("uid"),
					dataType: 0
				}
				var that = this;
				that.$u.debounce(() => {
					if (isChange) {
						zanSave(params).then(() => {
							isChange = false;
						})
					}
				}, 800)
			},
			reset(){
				this.d.data = [];
				this.queryParams = {
					content: undefined,
					pageSize: 10,
					pageNum: 1,
					queryUserId: uni.getStorageSync('uid'),
					dataType: '0'
				}
			}
		}
	}
</script>

<style>

</style>
